import Controller.PaiementController;
import Controller.ReservationController;
import Controller.SejourController;
import Model.Commis;
import Model.Reservation;
import Model.TestData;
import Model.Sejour;
import View.CreerReservation;
import View.DemarrerSejour;
import View.TerminerSejour;
import View.ModifierSejour;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class MainWindow extends JPanel {

    /**
     * Creates new form MainWindow
     */
    public MainWindow() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jPanelLogIn = new javax.swing.JPanel();
        lblUsername = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();
        btnLogIn = new javax.swing.JButton();
        lblPassword = new javax.swing.JLabel();
        jPanelMainWindow = new javax.swing.JPanel();
        this.jPanelMainWindow.setVisible(false);
        btnCreerReservation = new javax.swing.JButton();
        btnDemarrerSejour = new javax.swing.JButton();
        btnTerminerSejour = new javax.swing.JButton();
        btnModifSejour = new javax.swing.JButton();

        lblUsername.setText("Nom d'utilisateur");

        txtUsername.setNextFocusableComponent(txtPassword);

        txtPassword.setNextFocusableComponent(btnLogIn);
        txtPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPasswordKeyPressed(evt);
            }
        });

        btnLogIn.setText("Log In");
        btnLogIn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnLogInMouseClicked(evt);
            }
        });

        lblPassword.setText("Mot de passe");

        javax.swing.GroupLayout jPanelLogInLayout = new javax.swing.GroupLayout(jPanelLogIn);
        jPanelLogIn.setLayout(jPanelLogInLayout);
        jPanelLogInLayout.setHorizontalGroup(
            jPanelLogInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelLogInLayout.createSequentialGroup()
                .addContainerGap(41, Short.MAX_VALUE)
                .addGroup(jPanelLogInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelLogInLayout.createSequentialGroup()
                        .addGap(109, 109, 109)
                        .addComponent(btnLogIn))
                    .addGroup(jPanelLogInLayout.createSequentialGroup()
                        .addGroup(jPanelLogInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblUsername))
                        .addGap(49, 49, 49)
                        .addGroup(jPanelLogInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblPassword)
                            .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(25, 25, 25))
        );
        jPanelLogInLayout.setVerticalGroup(
            jPanelLogInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelLogInLayout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(jPanelLogInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUsername)
                    .addComponent(lblPassword))
                .addGap(18, 18, 18)
                .addGroup(jPanelLogInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addComponent(btnLogIn)
                .addContainerGap())
        );

        txtUsername.getAccessibleContext().setAccessibleName("");

        jPanelLogIn.setBounds(0, 0, 310, 120);
        jLayeredPane1.add(jPanelLogIn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jPanelMainWindow.setMinimumSize(new java.awt.Dimension(300, 110));

        btnCreerReservation.setText("Créer une réservation");
        btnCreerReservation.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnCreerReservationMouseClicked(evt);
            }
        });

        btnDemarrerSejour.setText("Démarrer un séjour");
        btnDemarrerSejour.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDemarrerSejourActionPerformed(evt);
            }
        });

        btnTerminerSejour.setText("Terminer un séjour");
        btnTerminerSejour.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnTerminerSejourMouseClicked(evt);
            }
        });

        btnModifSejour.setText("Modifier un séjour");
        btnModifSejour.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnModifSejourMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanelMainWindowLayout = new javax.swing.GroupLayout(jPanelMainWindow);
        jPanelMainWindow.setLayout(jPanelMainWindowLayout);
        jPanelMainWindowLayout.setHorizontalGroup(
            jPanelMainWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelMainWindowLayout.createSequentialGroup()
                .addContainerGap(24, Short.MAX_VALUE)
                .addGroup(jPanelMainWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnCreerReservation, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnDemarrerSejour, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanelMainWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnTerminerSejour, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnModifSejour, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(42, 42, 42))
        );
        jPanelMainWindowLayout.setVerticalGroup(
            jPanelMainWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMainWindowLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelMainWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnCreerReservation, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnModifSejour, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelMainWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDemarrerSejour, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnTerminerSejour, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        jPanelMainWindow.setBounds(0, 0, 320, 110);
        jLayeredPane1.add(jPanelMainWindow, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnLogInMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnLogInMouseClicked
        //loader la liste des commis
        
        LinkedList<Commis> listeCommis = TestData.getListeCommis();
        
        //prend le commis si le nom existe
        Commis commis = new Commis();
        boolean trouve= false;
        for (int i = 0; i< listeCommis.size() && !trouve;i++){
            commis = listeCommis.get(i);
            if (this.txtUsername.getText().equals(commis.getNom())){
                trouve = true;
            }
        }
        if(!trouve){
            JOptionPane.showMessageDialog(null,"Le nom d'utilisateur n'existe pas.");
        }
        else{
            //vérifier si c'est le bon password pour le user
            if (this.txtPassword.getText().toString().equals(commis.getPassword())){
                jPanelLogIn.setVisible(false);
                jPanelMainWindow.setVisible(true);
            }else{
                JOptionPane.showMessageDialog(null,"Le mot de passe est incorrect.");
            }
        }
    }//GEN-LAST:event_btnLogInMouseClicked

    private void txtPasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPasswordKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            btnLogInMouseClicked(new MouseEvent(btnLogIn, MouseEvent.MOUSE_CLICKED, 1, 0, 0, 0, 1, false));
        }
    }//GEN-LAST:event_txtPasswordKeyPressed

    private void btnCreerReservationMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCreerReservationMouseClicked
        creerReservation();
    }//GEN-LAST:event_btnCreerReservationMouseClicked

    private void btnDemarrerSejourActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDemarrerSejourActionPerformed
        demarrerSejour();
    }//GEN-LAST:event_btnDemarrerSejourActionPerformed

    private void btnTerminerSejourMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnTerminerSejourMouseClicked
        terminerSejour();
    }//GEN-LAST:event_btnTerminerSejourMouseClicked

    private void btnModifSejourMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnModifSejourMouseClicked
        ModifierSejour();
    }//GEN-LAST:event_btnModifSejourMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCreerReservation;
    private javax.swing.JButton btnDemarrerSejour;
    private javax.swing.JButton btnLogIn;
    private javax.swing.JButton btnModifSejour;
    private javax.swing.JButton btnTerminerSejour;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JPanel jPanelLogIn;
    private javax.swing.JPanel jPanelMainWindow;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables

    private void creerReservation() {
        Reservation reservation = new Reservation();
        ReservationController controller = new ReservationController();
        
        CreerReservation vueCreerReservation = new CreerReservation(controller);
        
        controller.addModel(reservation);
        
        reservation.initDefault();
        controller.addReservation();
        
        controller.addView(vueCreerReservation);
        
                
        JFrame displayFrame = new JFrame("Créer une réservation");
        displayFrame.getContentPane().add(vueCreerReservation, BorderLayout.CENTER);
        displayFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        displayFrame.pack();
        displayFrame.setVisible(true);
    }
    
    private void demarrerSejour(){
        Sejour sejour = new Sejour();
        SejourController controller = new SejourController();
        
        DemarrerSejour vueDemarrerSejour = new DemarrerSejour(controller);
        
        controller.addModel(sejour);
        
        //sejour.initDefault();
        //controller.addSejour();
        
        controller.addView(vueDemarrerSejour);
        
                
        JFrame displayFrame = new JFrame("Démarrer un séjour");
        displayFrame.getContentPane().add(vueDemarrerSejour, BorderLayout.CENTER);
        displayFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        displayFrame.pack();
        displayFrame.setVisible(true);
    }

    private void ModifierSejour(){
        SejourController controller = new SejourController();
        
        ModifierSejour vueModifierSejour = new ModifierSejour(controller);
        
        controller.addView(vueModifierSejour);
        
                
        JFrame displayFrame = new JFrame("Modifier un séjour");
        displayFrame.getContentPane().add(vueModifierSejour, BorderLayout.CENTER);
        displayFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        displayFrame.pack();
        displayFrame.setVisible(true);
    }
    
    private void terminerSejour() {
        PaiementController controller = new PaiementController();
        
        TerminerSejour vueTerminerSejour = new TerminerSejour(controller);
        
        controller.addView(vueTerminerSejour);
        
                
        JFrame displayFrame = new JFrame("Fin du séjour");
        displayFrame.getContentPane().add(vueTerminerSejour, BorderLayout.CENTER);
        displayFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        displayFrame.pack();
        displayFrame.setVisible(true);
    }
}
